package model;

/**
 * MovingPiece.java
 * Purpose: presents a ball piece that moves from a position to a destination over time
 * 
 * @author Hans Brems
 * @version 1.0 01/12/2011
 */

public class MovingPiece extends BallPiece {
	private Vector destination;
	private Vector velocity;
	private float speed = 10f;
	
	/** Constructs a new moving piece */
	public MovingPiece() {
	}
	
	/** Constructs a new moving piece */
	public MovingPiece(Vector position, Vector destination) {
		this.position = position;
		this.destination = destination;
		this.setVelocity();
	}
	
	/** Returns destination */
	public Vector getDestination() {
		return this.destination;
	}
	
	/** Sets the destination */
	public void setDestination(Vector destination) {
		this.destination = destination;
		this.setVelocity();
	}
	
	/** Returns true if the piece has reached its destination */
	public boolean isAtDestination() {
		return this.position.atDestination(destination, 5f);
	}
	
	/* Sets the velocity of the piece */
	public void setVelocity() {
		this.velocity = this.position.getVelocity(destination);
		this.velocity.toWorkUnits();
	}
	
	/** Updates the position of the piece */
	@Override
	public void update(double delta) {
		if (this.destination != null) {
			Vector movement = new Vector();
			movement.setX(this.velocity.getX() * this.speed * (float)delta);
			movement.setY(this.velocity.getY() * this.speed * (float)delta);
			this.position.add(movement);
			this.setVelocity();
		}
	}
}